package com.smilive.start.netty.netty;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @program: netty
 * @Date: 2022-01-05 15:20
 * @Author: smilive
 * @Description: Promise相当于一个容器，可以用于存放各个线程中的结果，然后让其他线程去获取该结果
 */
@Slf4j
public class NettyPromise {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        EventLoop eventLoop = eventLoopGroup.next();

        // 创建Promise对象，用于存放结果
        DefaultPromise<String> promise = new DefaultPromise<>(eventLoop);

        new Thread(() ->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            promise.setSuccess("smilive");
        }).start();

        System.out.println(Thread.currentThread().getName()+" " + promise.get());
    }
}
